1   // Licensed under the Apache License, Version 2.0 (the "License");
2   // you may not use this file except in compliance with the License.
3   // You may obtain a copy of the License at
4   //
5   //     http://www.apache.org/licenses/LICENSE-2.0
6   //
7   // Unless required by applicable law or agreed to in writing, software
8   // distributed under the License is distributed on an "AS IS" BASIS,
9   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  // See the License for the specific language governing permissions and
11  // limitations under the License.
12  
13  package org.apache.tapestry5.root;
14  
15  import org.apache.tapestry5.ContentType;
16  import org.apache.tapestry5.ioc.test.TestBase;
17  import org.testng.annotations.DataProvider;
18  import org.testng.annotations.Test;
19  
20  import java.util.List;
21  
22  public class ContentTypeTest extends TestBase
23  {
24      @Test
25      public void simple_equals()
26      {
27          ContentType master = new ContentType("text/html");
28  
29          assertFalse(master.equals(null));
30          assertFalse(master.equals(this));
31          assertTrue(master.equals(master));
32          assertTrue(master.equals(new ContentType("text/html")));
33          assertFalse(master.equals(new ContentType("foo/bar")));
34          assertFalse(master.equals(new ContentType("text/plain")));
35      }
36  
37      @Test
38      public void equals_with_parameters()
39      {
40          ContentType master = new ContentType("text/html;charset=utf-8");
41  
42          assertFalse(master.equals(new ContentType("text/html")));
43          assertTrue(master.equals(new ContentType("text/html;charset=utf-8")));
44          assertFalse(master.equals(new ContentType("text/html;charset=utf-8;foo=bar")));
45  
46          // Check that keys are case insensitive
47  
48          assertTrue(master.equals(new ContentType("text/html;Charset=utf-8")));
49  
50          master = new ContentType("text/html;foo=bar;biff=bazz");
51  
52          assertTrue(master.equals(new ContentType("text/html;foo=bar;biff=bazz")));
53          assertTrue(master.equals(new ContentType("text/html;Foo=bar;Biff=bazz")));
54          assertTrue(master.equals(new ContentType("text/html;biff=bazz;foo=bar")));
55      }
56  
57      @Test
58      public void parse_with_parameters() throws Exception
59      {
60          ContentType contentType = new ContentType("text/html;charset=utf-8");
61  
62          assertEquals(contentType.getBaseType(), "text");
63  
64          assertEquals(contentType.getSubType(), "html");
65  
66          assertEquals(contentType.getMimeType(), "text/html");
67  
68          List<String> parameterNames = contentType.getParameterNames();
69          assertEquals(parameterNames.size(), 1);
70  
71          assertEquals(parameterNames.get(0), "charset");
72  
73          assertEquals(contentType.getCharset(), "utf-8");
74  
75          assertTrue(contentType.hasParameters());
76  
77          String nonexistant = contentType.getParameter("nonexistant");
78  
79          assertTrue(nonexistant == null);
80      }
81  
82      @DataProvider
83      public Object[][] invalid_content_type_strings_data()
84      {
85          return new Object[][]{
86                  {""},
87                  {"foo/"},
88                  {"foo/bar;"},
89                  {"foo/bar;baz"},
90                  {"foo/bar;baz="},
91                  {"foo/bar;baz=biff;"}
92          };
93      }
94  
95      @Test(dataProvider = "invalid_content_type_strings_data")
96      public void invalid_content_type_strings(String input)
97      {
98          try
99          {
100             new ContentType(input);
101 
102             unreachable();
103         } catch (IllegalArgumentException ex)
104         {
105 
106         }
107 
108     }
109 
110     @Test
111     public void parse_without_parameters() throws Exception
112     {
113         ContentType contentType = new ContentType("text/html");
114 
115         assertEquals(contentType.getBaseType(), "text");
116 
117         assertEquals(contentType.getSubType(), "html");
118 
119         assertEquals(contentType.getMimeType(), "text/html");
120 
121         assertTrue(contentType.getParameterNames().isEmpty());
122 
123         assertFalse(contentType.hasParameters());
124     }
125 
126     @Test
127     public void unparse_with_parameters() throws Exception
128     {
129         ContentType contentType = new ContentType("text/html").withCharset("utf-8");
130 
131         assertEquals(contentType.toString(), "text/html;charset=utf-8");
132     }
133 
134     @Test
135     public void unparse_no_parameters() throws Exception
136     {
137         ContentType contentType = new ContentType("text/html");
138 
139         assertEquals(contentType.toString(), "text/html");
140     }
141 
142     @Test
143     public void add_charset() throws Exception
144     {
145         ContentType base = new ContentType("text/html");
146 
147         ContentType charset = base.withCharset("utf-8");
148 
149         assertTrue(charset.hasParameters());
150 
151         assertNotSame(base, charset);
152         assertNotEquals(base, charset);
153 
154         assertEquals(base.toString(), "text/html");
155         assertEquals(charset.toString(), "text/html;charset=utf-8");
156     }
157 }